<?php
/* $Id: groups.php,v 1.5 2003/10/08 07:15:12 tuxmonkey Exp $ */
/**
 * @package Issue-Tracker
 * @subpackage Groups
 */

if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) {
  print "Direct module access forbidden.";exit;}

if (!is_employee()) 
{
  redirect();
}

if (!empty($_GET['active']) and permission_check("Groups-Delete",$_GET['gid'])) 
{
    $update['active'] = $_GET['active'];
    $dbi->update("groups",$update,"WHERE gid='".$_GET['gid']."'");
    unset($update);
}

$links[] = array(
    "txt" => "[".$lg->add." ".$lg->group."]",
    "url" => "?module=groups&action=new",
    "img" => $_ENV['imgs']['new_group']
);

$links[] = array(
    "txt" => "[".$lg->copy." ".$lg->group."]",
    "url" => "?module=groups&action=copy",
    "img" => $_ENV['imgs']['copy_group']
);

// set type to show in titlebar
if ($_GET['start'] == "") 
{
    $type = "Default";
} else 
{
    $type = $_GET['start'];
}


	$sql  = "SELECT g.gid,g.name,a.cdprj,g.appli,g.grptp,g.active ";
	$sql .= "FROM groups AS g LEFT JOIN group_proj AS p ON g.gid = p.gid AND p.bukrs = 1 ";
	$sql .= "LEFT JOIN ac01 AS a ON p.idprj = a.idprj ";  

	if ($_GET['search'] == "true" and !empty($_POST['criteria'])) 
	{
		$sql .= "WHERE LOWER(g.name) LIKE LOWER('".$_POST['criteria']."') ";
	}
	else if ($_GET['start'] != "") 	
	{
		if ($_GET['start'] != "ALL") 
		{
		$sql .= "WHERE UPPER(g.name) LIKE '".$_GET['start']."%' ";
		}
	} 
	else 
	{
		$sql_groups = user_groups($_SESSION['userid'],TRUE);
		$sql .= "WHERE g.gid IN ($sql_groups) ";
	}
	// En fonction du filtre choisi
	if(!empty($_GET['sort']))
		$sql .= "ORDER BY ".$_GET['sort']." ".$_GET['order']." ";
	else
		$sql .= "ORDER BY g.name ASC ";

	// ordre de trie
	if(empty($_GET['sort']) || $_GET['order'] == 'DESC' )
		$smarty->assign('order','ASC');
	else
		$smarty->assign('order','DESC');

	$smarty->display("groups/groups.tpl");	
    $t_datas = $dbi->fetch_all($sql,"array");
	$o_grid = new Grid('GGROUPLIST',null,Msg('00',2,$lg->groups));
    
	if(is_array($t_datas))
	{
		foreach($t_datas as $key => $s_datas)
		{
			// 
			$gid   				= $s_datas['gid'];
			$s_edit['gid']   	= $s_datas['gid'];
			$link_value 		= $s_datas['name'];
			$link_destination 	= "?module=groups&action=view&gid=".$s_datas['gid'] ;
			$s_edit['lname'] 	=  "<a href='$link_destination'>$link_value</a> ";

			$s_edit['cdprj-FR'] = $s_datas['cdprj'];
			$macdp = $dbi->fetch_one("SELECT a.cdprj FROM group_proj AS g INNER JOIN ac01 AS a ON g.idprj = a.idprj WHERE g.bukrs = 2 AND g.gid = $gid ");
			$s_edit['cdprj-MA'] = $macdp;
			
			$s_edit['appli'] 	= $s_datas['appli'];
			$s_edit['grptp'] 	= $s_datas['grptp'];
			$s_edit['active'] = $s_datas['active'];
			$s_edit['iedit']  = "?module=groups&action=edit_info&gid=".$s_datas['gid'];
			$t_edit[] = $s_edit;
		}
	}
	$o_grid->setFieldCat(array('gid','lname','appli','grptp','active','iedit'));
	$o_grid->getFieldCat()->Add('cdprj','cdprj-FR',3);
	$o_grid->getFieldCat()->Add('cdprj','cdprj-MA',4);
	$o_grid->setDatas($t_edit);
	$o_grid->SetFieldAttr('gid',"noout",true);
	$o_grid->SetFieldAttr(array('cdprj-FR','cdprj-MA'),"oconv",false);
	$o_grid->SetFieldAttr('cdprj-FR',"SetDescr","Code (FR)");
	$o_grid->SetFieldAttr('cdprj-MA',"SetDescr","Code (MA)");
	$o_grid->SetFieldAttr('iedit','dtype','icon');	
	$o_grid->SetFieldAttr('active','dtype','flag');	
	$o_grid->SetFieldAttr('active','width',5);	
	$o_grid->SetFieldAttr('active','align',"center");	
	$anchor = new FoxAnchor("?module=groups",array("gid","active"));
	$o_grid->SetFieldAttr('active','SetAnchor',$anchor);	
	echo $o_grid->Display();

?>